package com.calsoft.Hops_Mobile_Tablet;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.calsoft.Hops_Mobile_Tablet.base.CardUI;
import com.calsoft.Hops_Mobile_Tablet.base.CardUIData;
import com.calsoft.Hops_Mobile_Tablet.component.TimePickerFragment;
import com.calsoft.Hops_Mobile_Tablet.sql.OpToursDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.sql.ProfileScheduleDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.TimeUtil;


/*
 * User: Minh Diep
 * Date: 7/8/13
 * Time: 6:18 PM
 */

public class BookNowActivity extends FragmentActivity implements TimePickerFragment.OnTimeSetListenter {

    private String landServiceCode;
    private String date;
    private int startTime;
    private int endTime;
    private Button startTimePickerBtn;
    private Button endTimePickerBtn;
    private Button confirmBookNowBtn;


    public static final int START_TIME_PICKER = 0;
    public static final int END_TIME_PICKER = 1;

    private ProfileScheduleDbAdapter profileScheduleDbAdapter;
    private OpToursDbAdapter opToursDbAdapter;
    private Cursor cursorItem;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_book_now);
        profileScheduleDbAdapter = new ProfileScheduleDbAdapter(getApplicationContext());
        opToursDbAdapter = new OpToursDbAdapter(getApplicationContext());
        profileScheduleDbAdapter.open();
        opToursDbAdapter.open();

        Bundle extras = getIntent().getExtras();
        landServiceCode = extras.getString(OpToursFragment.OP_TOURS_LAND_SERVICE_CODE, "");
        date = extras.getString(OpToursFragment.OP_TOURS_DATE, "");
        startTime = extras.getInt(OpToursFragment.OP_TOURS_START_TIME, 0);
        endTime = extras.getInt(OpToursFragment.OP_TOURS_END_TIME, 0);

        cursorItem = opToursDbAdapter.fetchServiceByLandServiceCode(landServiceCode);
        cursorItem.moveToFirst();

        startTimePickerBtn = (Button) findViewById(R.id.start_time_picker_btn);
        endTimePickerBtn = (Button) findViewById(R.id.end_time_picker_btn);
        confirmBookNowBtn = (Button) findViewById(R.id.book_now_confirm_btn);



        startTimePickerBtn.setText("Start Time: " + TimeUtil.getTimeStringOfDay(startTime));
        endTimePickerBtn.setText("End Time: " + TimeUtil.getTimeStringOfDay(endTime));
        startTimePickerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showStartTimePickerDialog(startTime / 60, startTime % 60);
            }
        });
        endTimePickerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEndTimePickerDialog(endTime / 60, endTime % 60);
            }
        });

        confirmBookNowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int insertRowId = (int) (profileScheduleDbAdapter.numberOfRows() + 1);
                String cardName = cursorItem.getString(5);
                String cardDesc = cursorItem.getString(4);
                String imageTag = cursorItem.getString(7);

                CardUIData cardUIData = new CardUIData(insertRowId, CardUI.TYPE_SERVICE, cardName, startTime, endTime, cardDesc, landServiceCode, imageTag);
                profileScheduleDbAdapter.insertCardUIData(cardUIData, date);

                Intent i = new Intent(BookNowActivity.this, MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);


            }
        });
    }

    private void showEndTimePickerDialog(int hour, int minute) {
        DialogFragment newFragment = new TimePickerFragment(END_TIME_PICKER, hour, minute);
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }


    public void showStartTimePickerDialog(int hour, int minute) {
        DialogFragment newFragment = new TimePickerFragment(START_TIME_PICKER, hour, minute);
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    @Override
    public void onTimeSet(int type, int hourOfDay, int minute) {
        int minuteOfDay = hourOfDay * 60 + minute;
        if (type == START_TIME_PICKER){
            if (minuteOfDay < startTime || minuteOfDay > endTime){
                Toast.makeText(this, "Your Time Set is Invalid!", Toast.LENGTH_LONG).show();
            } else {
                startTimePickerBtn.setText("Start Time: " + TimeUtil.getTimeStringOfDay(startTime));
                startTime = minuteOfDay;
            }
        } else if (type == END_TIME_PICKER){
            if (minuteOfDay < startTime || minuteOfDay > endTime){
                Toast.makeText(this, "Your Time Set is Invalid!", Toast.LENGTH_LONG).show();
            } else {
                endTimePickerBtn.setText("End Time: " + TimeUtil.getTimeStringOfDay(endTime));
                endTime = minuteOfDay;
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        profileScheduleDbAdapter.close();
        opToursDbAdapter.close();
    }
}